<?php
/*
 *新闻管理
 *author 王建 
 */

class NewsAction extends BaseAction{
	public $d = '';
	function __construct(){
		parent::__construct();
		$this->d = D("Admin/News");;
	}
	function index(){
		$action = $this->_request("action");	
		$action_array = array("show","ajax_data" , "upload_pic","upload");
		$action = !in_array($action,$action_array)?'show':$action ;
		if($action == 'show'){
			$typeid = intval($this->_request("typeid"));
			$list = $this->d->make_option_data();
			$this->assign("category" , $list) ;
			$this->assign("typeid" , $typeid) ;
			$this->display();
		}elseif($action == 'ajax_data'){
			$this->ajax_data();
		}elseif($action == "upload_pic"){
			$this->upload_pic();
		}elseif($action == "upload"){
			$this->upload();
		}
	}
	//ajax get data
	private function ajax_data(){

		$page = intval($this->_request("page"  ));
		$list = $this->d->queryNewsList($page ,30 , 
				array(
							'title'=>trim($this->_request("title")) ,
							'flag'=>trim($this->_request("flag")) ,
							'status'=>trim($this->_request("status")) ,
						 	'typeid'=>trim($this->_request("typeid")) ,
						) );

		echo result_to_towf_new($list['list'], 1, '成功', $list['page_string']) ;
	}
	private function upload_pic(){
		$res = $this->d->upload_news_pic();
		if($res['status']){
			echo result_to_towf_new(array('pic'=>$res['pic']) , 1, $res['message'], null);
			die();
		}else{
			echo result_to_towf_new($res, 0,$res['message'], null);
			die() ;
		}
	}
	//上传编辑器的图片
	private function upload(){
		$res = $this->d->upload_kindeditor();
		if($res['status']){
			echo json_encode(array('error' => 0, 'message' => $res['message'] , 'url'=>$res['url']));
		}else{
			echo json_encode(array('error' =>1, 'message' => $res['message'] ));
		}
		
	}
	//添加新闻咨询
	function add(){
		$action = $this->_request("action");		
		$action_array = array("add","doadd");
		$action = !in_array($action,$action_array)?'add':$action ;	
		if($action == 'add'){
			$list = $this->d->make_option_data();
			$this->assign("category" , $list) ;
			$this->display();		
		}elseif($action =='doadd'){
			$this->doadd();
		}
	}
	//处理添加
	private function doadd(){
		$res = $this->d->insertNews();
		if($res['status']){
			echo result_to_towf_new(null, 1, "success", null);
		}else{
			echo result_to_towf_new(null, 0, $res['message'], null);
			exit ;
		}	
	}
	//编辑页面
	function edit(){
		$action = $this->_request("action");		
		$action_array = array("edit","doedit" , "doStatus");
		$action = !in_array($action,$action_array)?'edit':$action ;		
		if($action == 'edit'){
			$list = $this->d->make_option_data();
			
			$id = intval($this->_request("id"));
			$info = $this->d->getNewDataById($id);
			$this->assign("category" , $list) ;
			$this->assign("info" , $info) ;
			$this->assign("id" , $id) ;
			$this->display();		

		}elseif($action == 'doedit'){
			$this->doedit();
		}elseif($action=="doStatus"){
			$this->doStatus();
		}

	}
	//处理编辑数据
	private function doedit(){
		$res = $this->d->editNews();
		if($res['status']){
			echo result_to_towf_new(null, 1, $res['message'], null);
		}else{
			echo result_to_towf_new(null, 0, $res['message'], null);
			exit ;
		}
	}

	private function doStatus(){
		$data = array(
			'id'=>	intval($this->_request("id" )) , 
			'status'=>$this->_request("status" ) , 			
		);
		$res = $this->d->doStatus($data);
		echo result_to_towf_new(null, $res['status'], $res['message'], null);
	}
	
	//删除
    public function del(){
        $data = array(
            'id'=>  intval($this->_request("id" )) , 
                    
        );
        $res = $this->d->del($data);
        echo result_to_towf_new(null, $res['status'], $res['message'], null);
    }
}